import demo.MySingleList;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(33);
        mySingleList.addLast(1);
        mySingleList.addLast(55);
        mySingleList.addLast(66);
        MySingleList.ListNode ret = mySingleList.contains2(66);
        ret.next = mySingleList.contains2(1);
        MySingleList.ListNode ret2 = mySingleList.detectCycle();
        System.out.println(ret2.val);
    }
    public static void main5(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(44);
        mySingleList.addLast(33);
        mySingleList.addLast(11);
        mySingleList.addLast(55);
        mySingleList.addLast(66);
        MySingleList.ListNode ret = mySingleList.contains2(66);
        ret.next = mySingleList.contains2(11);
        boolean flag = mySingleList.hasCycle(mySingleList.head);
        System.out.println(flag);
    }
    /**
     * 相交链表
     * @param headA
     * @param headB
     * @return
     */
    public static MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA, MySingleList.ListNode headB) {
        MySingleList.ListNode pl = headA;//假设pl永远指向最长链表
        MySingleList.ListNode ps = headB;//假设ps永远指向最短链表
        int lenA = 0;
        int lenB = 0;
        //1.求长度
        while(pl != null) {
            lenA++;
            pl = pl.next;
        }

        while(ps != null) {
            lenB++;
            ps = ps.next;
        }

        //2.走差值步
        pl = headA;
        ps = headB;
        int len = lenA - lenB;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }

        while(len > 0) {
            pl = pl.next;
            len--;
        }

        //3.判断是否相交
        while(pl != null && pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        if(pl == null && pl == ps) {//判断不相加情况(都为null)
            return null;
        }
        return ps;
    }
    private static void createCut(MySingleList.ListNode headA,MySingleList.ListNode headB) {
        headB.next = headA.next.next;//仅对可测试特殊例子.
    }
    public static void main4(String[] args) {
//        MySingleList mySingleList = new MySingleList();
//        mySingleList.addLast(11);
//        mySingleList.addLast(22);
//        mySingleList.addLast(44);
//        MySingleList mySingleList2 = new MySingleList();
//        mySingleList2.addLast(1);
//        mySingleList2.addLast(2);
//        mySingleList2.addLast(4);
//        MySingleList mySingleList3 = new MySingleList();
//        mySingleList3.addLast(13);
//        mySingleList3.addLast(22);
//        mySingleList3.addLast(41);
//        MySingleList.ListNode ret1 = mySingleList.contains(44);
//        MySingleList.ListNode ret2 = mySingleList2.contains(1);
//        MySingleList.ListNode ret3 = mySingleList3.contains(41);
//        ret1.next = ret2;
//        ret3.next = ret2;
//        MySingleList.ListNode ret = getIntersectionNode(mySingleList.head,mySingleList2.head);
//        System.out.println(ret.val);
        //使用createCut进行测试
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(15);
        mySingleList.addLast(16);
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(4);
        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(3);
        mySingleList2.addLast(1);
        mySingleList2.addLast(2);
        mySingleList2.addLast(4);

        createCut(mySingleList.head,mySingleList2.head);

        MySingleList.ListNode ret = getIntersectionNode(mySingleList.head,mySingleList2.head);
        System.out.println(ret.val);
    }
    public static void main3(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(12);
        mySingleList.addLast(23);
        mySingleList.addLast(34);
        mySingleList.addLast(23);
        mySingleList.addLast(111);

        boolean ret = mySingleList.chkPalindrome();
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(44);
        mySingleList.addLast(44);
        mySingleList.addLast(44);
        mySingleList.addLast(44);
        mySingleList.addLast(44);
        mySingleList.display();

        mySingleList.removeAllKey(44);
        mySingleList.display();
    }
    public static void main1(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(44);
        mySingleList.addLast(33);
        mySingleList.addLast(44);
        mySingleList.addLast(55);
        mySingleList.addLast(66);
        mySingleList.display();

        MySingleList.ListNode ret = mySingleList.partition(30);
        mySingleList.display();
    }
}
